package cn.ds.assets.module.assets.controller.admin.dataQualityRuleTemplate;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.ds.assets.framework.common.pojo.PageResult;
import cn.ds.assets.framework.common.pojo.CommonResult;
import static cn.ds.assets.framework.common.pojo.CommonResult.success;

import cn.ds.assets.framework.excel.core.util.ExcelUtils;

import cn.ds.assets.framework.operatelog.core.annotations.OperateLog;
import static cn.ds.assets.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.ds.assets.module.assets.controller.admin.dataQualityRuleTemplate.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.dataQualityRuleTemplate.BaseDataQualityRuleTemplateDO;
import cn.ds.assets.module.assets.convert.dataQualityRuleTemplate.BaseDataQualityRuleTemplateConvert;
import cn.ds.assets.module.assets.service.dataQualityRuleTemplate.BaseDataQualityRuleTemplateService;

//@Tag(name = "管理后台 - 数据质量规则模板关联")
//@RestController
//@RequestMapping("/assets/base-data-quality-rule-template")
@Validated
public class BaseDataQualityRuleTemplateController {

    @Resource
    private BaseDataQualityRuleTemplateService baseDataQualityRuleTemplateService;

    @PostMapping("/create")
    @Operation(summary = "创建数据质量规则模板关联")
    @PreAuthorize("@ss.hasPermission('assets:base-data-quality-rule-template:create')")
    public CommonResult<Long> createBaseDataQualityRuleTemplate(@Valid @RequestBody BaseDataQualityRuleTemplateCreateReqVO createReqVO) {
        return success(baseDataQualityRuleTemplateService.createBaseDataQualityRuleTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据质量规则模板关联")
    @PreAuthorize("@ss.hasPermission('assets:base-data-quality-rule-template:update')")
    public CommonResult<Boolean> updateBaseDataQualityRuleTemplate(@Valid @RequestBody BaseDataQualityRuleTemplateUpdateReqVO updateReqVO) {
        baseDataQualityRuleTemplateService.updateBaseDataQualityRuleTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据质量规则模板关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('assets:base-data-quality-rule-template:delete')")
    public CommonResult<Boolean> deleteBaseDataQualityRuleTemplate(@RequestParam("id") Long id) {
        baseDataQualityRuleTemplateService.deleteBaseDataQualityRuleTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据质量规则模板关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('assets:base-data-quality-rule-template:query')")
    public CommonResult<BaseDataQualityRuleTemplateRespVO> getBaseDataQualityRuleTemplate(@RequestParam("id") Long id) {
        BaseDataQualityRuleTemplateDO baseDataQualityRuleTemplate = baseDataQualityRuleTemplateService.getBaseDataQualityRuleTemplate(id);
        return success(BaseDataQualityRuleTemplateConvert.INSTANCE.convert(baseDataQualityRuleTemplate));
    }

    @GetMapping("/list")
    @Operation(summary = "获得数据质量规则模板关联列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('assets:base-data-quality-rule-template:query')")
    public CommonResult<List<BaseDataQualityRuleTemplateRespVO>> getBaseDataQualityRuleTemplateList(@RequestParam("ids") Collection<Long> ids) {
        List<BaseDataQualityRuleTemplateDO> list = baseDataQualityRuleTemplateService.getBaseDataQualityRuleTemplateList(ids);
        return success(BaseDataQualityRuleTemplateConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据质量规则模板关联分页")
    @PreAuthorize("@ss.hasPermission('assets:base-data-quality-rule-template:query')")
    public CommonResult<PageResult<BaseDataQualityRuleTemplateRespVO>> getBaseDataQualityRuleTemplatePage(@Valid BaseDataQualityRuleTemplatePageReqVO pageVO) {
        PageResult<BaseDataQualityRuleTemplateDO> pageResult = baseDataQualityRuleTemplateService.getBaseDataQualityRuleTemplatePage(pageVO);
        return success(BaseDataQualityRuleTemplateConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据质量规则模板关联 Excel")
    @PreAuthorize("@ss.hasPermission('assets:base-data-quality-rule-template:export')")
    @OperateLog(type = EXPORT)
    public void exportBaseDataQualityRuleTemplateExcel(@Valid BaseDataQualityRuleTemplateExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BaseDataQualityRuleTemplateDO> list = baseDataQualityRuleTemplateService.getBaseDataQualityRuleTemplateList(exportReqVO);
        // 导出 Excel
        List<BaseDataQualityRuleTemplateExcelVO> datas = BaseDataQualityRuleTemplateConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "数据质量规则模板关联.xls", "数据", BaseDataQualityRuleTemplateExcelVO.class, datas);
    }

}
